package com.yunze.apiCommon.upstreamAPI.JuMeng;

import com.alibaba.fastjson.JSONObject;
import com.yunze.apiCommon.utils.HttpUtil;
import com.yunze.apiCommon.utils.UrlUtil;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * JuMeng API
 */
public class JuMeng_Api {


	//构造赋值
	public JuMeng_Api(Map<String, Object> init_map){

		appid = init_map.get("cd_username").toString();
		 // 秘钥密码 	
		appSecret = Base64.getEncoder().encodeToString(init_map.get("cd_pwd").toString().getBytes());
		setToken();
    }
	
	// 服务器请求地址
	protected   String server_Ip = "http://api.zwytco.com";
	// appid 					  
	protected  String appid = null;
	// 秘钥密码 	
	protected  String appSecret = null;

	protected  String token = null;

	protected Map<String, String> headers = null;



	public static void main(String[] args) {
		Map<String, Object> map_1 = new HashMap<String, Object>();

		map_1.put("cd_username","");
		map_1.put("cd_pwd","");
		map_1.put("cd_key","");
		JuMeng_Api Qy = new JuMeng_Api(map_1);

		System.out.println(Qy.changeStatus("","1"));


	}

	public void setToken(){
		String api_url = server_Ip + "/v5/auth/token";
		JSONObject params = null;
		params = new JSONObject();
		params.put("appid",appid);
		params.put("appSecret",appSecret);
		//请求头部参数
		String res = HttpUtil.post(api_url, params.toJSONString());
		JSONObject obj =JSONObject.parseObject(res);
		token = obj.getJSONObject("data").get("access_token").toString();

		headers = new HashMap<String, String>();
		headers.put("Content-Type", "application/json");
		headers.put("Authorization", obj.getJSONObject("data").get("token_type").toString()+" "+token);

	}



	public String getCardInfo(String accessNumber){

		String api_url = server_Ip + "/v5/card/info";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		String param_url = UrlUtil.getUrl(api_url, params);
		String res = HttpUtil.get(param_url, headers);
		return res ;
	}



	public String getImei(String accessNumber){
		String api_url = server_Ip + "/v5/card/imei";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		String param_url = UrlUtil.getUrl(api_url, params);
		String res = HttpUtil.get(param_url, headers);
		return res ;
	}

	public String getStatus(String accessNumber){
		String api_url = server_Ip + "/v5/card/status";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		String param_url = UrlUtil.getUrl(api_url, params);
		String res = HttpUtil.get(param_url, headers);
		return res ;
	}

	public String getFlow(String accessNumber){
		String api_url = server_Ip + "/v5/card/flow";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		String param_url = UrlUtil.getUrl(api_url, params);
		String res = HttpUtil.get(param_url, headers);
		return res ;
	}


	/*
	开关机状态
	 */
	public String getStatusActivity(String accessNumber){
		String api_url = server_Ip + "/v5/card/statusActivity";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		String param_url = UrlUtil.getUrl(api_url, params);
		String res = HttpUtil.get(param_url, headers);
		return res ;
	}

	/**
	 *
	 * @param accessNumber
	 * @param status 0:申请停机(已激活转已停机) 1:申请复机(已停机转已激活)
	 * @return
	 */
	public String changeStatus(String accessNumber,String status){
		String api_url = server_Ip + "/v5/oper/status";
		JSONObject params = new JSONObject();
		params.put("accessNumber",accessNumber);
		params.put("status",status);
		String res = HttpUtil.post(api_url,params.toJSONString(), headers);
		return res ;
	}


}
